﻿-- Передача дочерных записей в другом ветве и удаление текушего
CREATE PROCEDURE [dbo].[axs_TreeView_RemoveTreeMoveSubs]
	@iTableID [int] = 0,
	@CodeID [int],
	@Newmgrid [int]
WITH EXECUTE AS CALLER
AS
IF @iTableID BETWEEN 1 AND 3
BEGIN
	DECLARE @dynsql nvarchar(4000),@cTableName varchar(40)
	SET @cTableName = 
		CASE @iTableID 
		WHEN 1 THEN 'axsPost'  
		WHEN 2 THEN 'axsDep'  
		WHEN 3 THEN 'axsPlace'
		WHEN 4 THEN 'axsPRType'  END 
	SET @dynsql = 
		'UPDATE E'+char(13)+
		'SET mgrid = '+convert(varchar,@Newmgrid)+char(13)+
		'FROM '+@cTableName+' AS E JOIN '+@cTableName+' AS M'+char(13)+
		'  ON E.mgrid = M.CodeID'+char(13)+
		'WHERE M.CodeID = '+convert(varchar,@CodeID)+char(13)+
		'DELETE FROM '+@cTableName+char(13)+
		'WHERE CodeID = '+convert(varchar,@CodeID)
	EXEC(@dynsql) -- PRINT @dynsql
END


